﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using System.Windows.Markup;
using System.Windows.Media;
using CrazyWorship1.Particles;

namespace CrazyWorship1.Creators
{
    public class CreateAllCenteredWithParticles:CreateAllBase
    {

        System.Windows.Threading.DispatcherTimer frameTimer;

        private Point3D spawnPoint;
        private double elapsed;
        private double totalElapsed;
        private int lastTick;
        private int currentTick;

       
        private ParticleSystemManager pm;

        private Random rand;



        public override void SetupPanes()
        {
            CurrentState.Viewers[0].Background = Brushes.Black;
            CurrentState.Viewers[1].Background = Brushes.Black;
          
            foreach (DataRowView a in CurrentState.CurrentLyrics)
            {
                SongDB.LyricsRow b = (SongDB.LyricsRow)a.Row;
                CreateTextPanel(TextModelGroup, b.LyricID, b.Text, b.Style, new Point3D(0, 0, 0), Rotation3D.Identity, new Point3D(0, 0, 0),new Vector3D(1,1,1));
                SetIn(b.LyricID, b.InStoryBoard);
                SetOut(b.LyricID, b.OutStoryBoard);
            }
            SetSongIn(CurrentState.CurrentSong.InStoryBoard);
            SetSongOut(CurrentState.CurrentSong.OutStoryBoard);

            frameTimer = new System.Windows.Threading.DispatcherTimer();
            frameTimer.Tick += OnFrame;
            frameTimer.Interval = TimeSpan.FromSeconds(1.0 / 60.0);
            frameTimer.Start();

            this.spawnPoint = new Point3D(0.0, 0.0, -5);
            this.lastTick = Environment.TickCount;

            pm = new ParticleSystemManager();
            Model3DGroup m=new Model3DGroup();
            this.BackgroundModelGroup.Children.Add(m);
            m.Children.Add(pm.CreateParticleSystem(5, Colors.Gray));
        

            rand = new Random(this.GetHashCode());

           // this.KeyDown += new System.Windows.Input.KeyEventHandler(Window1_KeyDown);
            //this.Cursor = System.Windows.Input.Cursors.None;
        }

        protected override void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            
        }
        double sizefactor = 1;
        double lifefactor = 2.5;
        double speedfactor = 0.1;
        double distance = 0.3;
        private void OnFrame(object sender, EventArgs e)
        {
            // Calculate frame time;
            this.currentTick = Environment.TickCount;
            this.elapsed = (double)(this.currentTick - this.lastTick) / 1000.0;
            this.totalElapsed += this.elapsed;
            this.lastTick = this.currentTick;
          

            pm.Update((float)elapsed);
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Red, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Orange, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Silver, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Gray, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Red, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Orange, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Silver, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Gray, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Red, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Yellow, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Silver, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());
            pm.SpawnParticle(this.spawnPoint, speedfactor, Colors.Yellow, sizefactor * rand.NextDouble(), lifefactor * rand.NextDouble());

            c = Math.Cos(this.totalElapsed);
            s = Math.Sin(this.totalElapsed);

            this.spawnPoint.X= s *distance ;
            this.spawnPoint.Y = c * distance ;
        }
        double c;
        double s;
       

      

       
    }
}
